package com.eportal.struts.action;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import com.eportal.ORM.UserView;
import com.eportal.ORM.WlExpressInfo;
import com.eportal.core.BaseAction;
import com.eportal.core.Page;
import com.eportal.core.Util;
import com.eportal.service.AdminService;

public class ExpressAction extends BaseAction {
	AdminService service;
	AdminService serviceOracle;
	private Page page;
	private String start = "0";
	private String limit = "30";
	private boolean success;
	private String message;

	private String payid;
	private String expressSeq;
	private String expressOrigin;
	private String expressDestination;
	private String expressFromName;
	private String expressToName;
	private String expressFromAddress;
	private String expressToAddress;
	private String expressFromTel;
	private String expressToNo;
	private String expressFromNo;
	private String expressToTel;
	private String expressGoods;
	private Long expressGoodsAmount;
	private String expressBankCard;
	private String expressBankName;
	private String expressBankAccname;
	private Timestamp expressAppdt;
	private Timestamp expressUpdt;
	private String expressAppusr;
	private String expressUpusr;
	private String expressPayState;
	private String expressGoodsState;
	private String expressLog;
	private String expressGoodsMan;
	private String expressOrgcode;
	private String expressId;
	private String expressNewGoodType;
	private WlExpressInfo wlExpressInfo;
	
	private boolean smsFrom;
	private boolean smsTo;

	public boolean isSmsFrom() {
		return smsFrom;
	}

	public void setSmsFrom(boolean smsFrom) {
		this.smsFrom = smsFrom;
	}

	public boolean isSmsTo() {
		return smsTo;
	}

	public void setSmsTo(boolean smsTo) {
		this.smsTo = smsTo;
	}


	public WlExpressInfo getWlExpressInfo() {
		return wlExpressInfo;
	}

	public void setWlExpressInfo(WlExpressInfo wlExpressInfo) {
		this.wlExpressInfo = wlExpressInfo;
	}

	public String getExpressId() {
		return expressId;
	}

	public void setExpressId(String expressId) {
		this.expressId = expressId;
	}

	public String getExpressNewGoodType() {
		return expressNewGoodType;
	}

	public void setExpressNewGoodType(String expressNewGoodType) {
		this.expressNewGoodType = expressNewGoodType;
	}

	public String getPayid() {
		return payid;
	}

	public void setPayid(String payid) {
		this.payid = payid;
	}

	public String getExpressOrigin() {
		return expressOrigin;
	}

	public void setExpressOrigin(String expressOrigin) {
		this.expressOrigin = expressOrigin;
	}

	public String getExpressDestination() {
		return expressDestination;
	}

	public void setExpressDestination(String expressDestination) {
		this.expressDestination = expressDestination;
	}

	public String getExpressFromName() {
		return expressFromName;
	}

	public void setExpressFromName(String expressFromName) {
		this.expressFromName = expressFromName;
	}

	public String getExpressToName() {
		return expressToName;
	}

	public void setExpressToName(String expressToName) {
		this.expressToName = expressToName;
	}

	public String getExpressFromAddress() {
		return expressFromAddress;
	}

	public void setExpressFromAddress(String expressFromAddress) {
		this.expressFromAddress = expressFromAddress;
	}

	public String getExpressToAddress() {
		return expressToAddress;
	}

	public void setExpressToAddress(String expressToAddress) {
		this.expressToAddress = expressToAddress;
	}

	public String getExpressFromTel() {
		return expressFromTel;
	}

	public void setExpressFromTel(String expressFromTel) {
		this.expressFromTel = expressFromTel;
	}

	public String getExpressToNo() {
		return expressToNo;
	}

	public void setExpressToNo(String expressToNo) {
		this.expressToNo = expressToNo;
	}

	public String getExpressFromNo() {
		return expressFromNo;
	}

	public void setExpressFromNo(String expressFromNo) {
		this.expressFromNo = expressFromNo;
	}

	public String getExpressToTel() {
		return expressToTel;
	}

	public void setExpressToTel(String expressToTel) {
		this.expressToTel = expressToTel;
	}

	public String getExpressGoods() {
		return expressGoods;
	}

	public void setExpressGoods(String expressGoods) {
		this.expressGoods = expressGoods;
	}

	public Long getExpressGoodsAmount() {
		return expressGoodsAmount;
	}

	public void setExpressGoodsAmount(Long expressGoodsAmount) {
		this.expressGoodsAmount = expressGoodsAmount;
	}

	public String getExpressBankCard() {
		return expressBankCard;
	}

	public void setExpressBankCard(String expressBankCard) {
		this.expressBankCard = expressBankCard;
	}

	public String getExpressBankName() {
		return expressBankName;
	}

	public void setExpressBankName(String expressBankName) {
		this.expressBankName = expressBankName;
	}

	public String getExpressBankAccname() {
		return expressBankAccname;
	}

	public void setExpressBankAccname(String expressBankAccname) {
		this.expressBankAccname = expressBankAccname;
	}

	public Timestamp getExpressAppdt() {
		return expressAppdt;
	}

	public void setExpressAppdt(Timestamp expressAppdt) {
		this.expressAppdt = expressAppdt;
	}

	public Timestamp getExpressUpdt() {
		return expressUpdt;
	}

	public void setExpressUpdt(Timestamp expressUpdt) {
		this.expressUpdt = expressUpdt;
	}

	public String getExpressAppusr() {
		return expressAppusr;
	}

	public void setExpressAppusr(String expressAppusr) {
		this.expressAppusr = expressAppusr;
	}

	public String getExpressUpusr() {
		return expressUpusr;
	}

	public void setExpressUpusr(String expressUpusr) {
		this.expressUpusr = expressUpusr;
	}

	public String getExpressPayState() {
		return expressPayState;
	}

	public void setExpressPayState(String expressPayState) {
		this.expressPayState = expressPayState;
	}

	public String getExpressGoodsState() {
		return expressGoodsState;
	}

	public void setExpressGoodsState(String expressGoodsState) {
		this.expressGoodsState = expressGoodsState;
	}

	public String getExpressLog() {
		return expressLog;
	}

	public void setExpressLog(String expressLog) {
		this.expressLog = expressLog;
	}

	public String getExpressGoodsMan() {
		return expressGoodsMan;
	}

	public void setExpressGoodsMan(String expressGoodsMan) {
		this.expressGoodsMan = expressGoodsMan;
	}

	public String getExpressOrgcode() {
		return expressOrgcode;
	}

	public void setExpressOrgcode(String expressOrgcode) {
		this.expressOrgcode = expressOrgcode;
	}

	public String getExpressSeq() {
		return expressSeq;
	}

	public void setExpressSeq(String expressSeq) {
		this.expressSeq = expressSeq;
	}

	public AdminService getService() {
		return service;
	}

	public void setService(AdminService service) {
		this.service = service;
	}

	public AdminService getServiceOracle() {
		return serviceOracle;
	}

	public void setServiceOracle(AdminService serviceOracle) {
		this.serviceOracle = serviceOracle;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getLimit() {
		return limit;
	}

	public void setLimit(String limit) {
		this.limit = limit;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getExpressinfo() {
		LOG.info("读取物流信息数据...");
		UserView m = (UserView) getRequest().getSession().getAttribute(
				"mpay_user");
		int s = Integer.valueOf(start);
		int l = Integer.valueOf(limit);
		String sql = "";
		if (m.getIsAll() == 1) {
			sql += " where '1'='1' ";
		} else {
			sql += " where express_Orgcode = '" + m.getShopId() + "' ";
		}
		expressSeq = expressSeq == null ? "" : expressSeq.trim();
		if (!expressSeq.equals("")) {
			sql += " and express_Seq like '%" + expressSeq + "%' ";
		}
		// System.out.println("334:::"+expressGoodsState);
		expressGoodsState = expressGoodsState == null ? "" : expressGoodsState
				.trim();
		if (!expressGoodsState.equals("")) {
			sql += " and express_Goods_State = '" + expressGoodsState + "' ";
		}
		int start = 1;
		if (s != 0)
			start = s / 30 + 1;
		// System.out.println(sql);
		sql += " order by express_appdt desc ";
		System.out.println(sql);
		List userList = serviceOracle.query("from ViewExpressInfo" + sql,
				start, l);
		// List tmp = new ArrayList<ViewExpressInfo>();
		// // System.out.println(userList.size());
		// for (int i = 0; i < userList.size(); i++) {
		// System.out.println(((ViewExpressInfo)userList.get(i)));
		// tmp.add(((ViewExpressInfo)userList.get(i)).getId());
		// }
		page = new Page();
		page.setRoot(userList);
		page.setTotalProperty(serviceOracle
				.countQuery("select count(*) from ViewExpressInfo" + sql));
		page.setStart(++s);
		page.setLimit(l = l == 0 ? 30 : l);
		return SUCCESS;
	}

	public String addexpress() {
		LOG.info("添加物流单...");
		UserView mpay = (UserView) getRequest().getSession().getAttribute(
				"mpay_user");
		List list = serviceOracle
				.query(" from WtZfxtInfo where trim(ZFXT_CYZQC) = '"
						+ expressBankName + "'");
		if (list.size() != 1) {
			message = "银行检索错误，请重新输入！";
			success = false;
			return SUCCESS;
		}
		WlExpressInfo w = new WlExpressInfo();
		w.setExpressAppdt(new Date());
		w.setExpressAppusr(mpay.getMpayUserid());
		w.setExpressBankAccname(expressBankAccname);
		w.setExpressBankCard(expressBankCard);
		w.setExpressBankName(expressBankName);
		w.setExpressDestination(expressDestination);
		w.setExpressFromAddress(expressFromAddress);
		w.setExpressFromName(expressFromName);
		w.setExpressFromNo(expressFromNo);
		w.setExpressFromTel(expressFromTel);
		w.setExpressGoods(expressGoods);
		w.setExpressOrgcode(mpay.getShopId());
		w.setExpressUpusr(mpay.getMpayUserid());
		w.setExpressUpdt(new Date());
		w.setExpressGoodsState("0");
		w.setExpressPayState("0");
		w.setExpressOrigin(expressOrigin);
		w.setExpressToTel(expressToTel);
		w.setExpressToNo(expressToNo);
		w.setExpressToName(expressToName);
		w.setExpressToAddress(expressToAddress);
		w.setExpressGoodsAmount(expressGoodsAmount * 100);
		payid = Util.getCurrentDate("MMdd") + Util.addZero(getTrace() + "", 6);
		w.setExpressGoodsMan("");
		w.setExpressLog("");
		w.setExpressSeq(payid);
		try {
			serviceOracle.save(w);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message = "新增物流单失败";
			success = false;
			return SUCCESS;
		}
		System.out.println(smsFrom+" "+smsTo);
		String msg = "成功!,物流单号:"+w.getExpressSeq()+",收货人："+w.getExpressToName().trim()+",货品:"+w.getExpressGoods();
		if(smsFrom){
			try {
				Util.sendSMS(msg, w.getExpressFromTel().trim());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(smsTo){
			try {
				Util.sendSMS(msg, w.getExpressToTel().trim());
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		message = "新增物流单成功";
		success = true;
		return SUCCESS;
	}

	public String changeType() {
		LOG.info("变更物流单状态...");
		expressId = expressId.endsWith(",") ? expressId.substring(0, expressId
				.lastIndexOf(",")) : expressId;
		expressId = "'" + expressId.replace(",", "','") + "'";
		String Hql = "update WlExpressInfo set express_goods_state = '"
				+ expressNewGoodType + "' where express_seq in (" + expressId
				+ ")";
		System.out.println("ddfdf:"+Hql);
		try {
			int i = serviceOracle.update(Hql);
			message = "操作成功!";
			success = true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			message = "操作失败!";
			success = false;
		}
		return SUCCESS;
	}
	
	public String findbySeq(){
		LOG.info("读取物流单信息...");
		wlExpressInfo=(WlExpressInfo) serviceOracle.loadById(WlExpressInfo.class, expressSeq);
		success = true;
		return SUCCESS;
	}

	public int getTrace() {
		int i = 0;
		List tmp = serviceOracle
				.query(" from WlExpressInfo order by to_number(substr(Express_seq,4,12)) desc ");
		if (tmp.size() > 0) {
			WlExpressInfo w = (WlExpressInfo) tmp.get(0);
			// System.out.println(w.getPayZfId()+" "+w.getPayZfId().substring(4));
			if (w.getExpressSeq() != null && !w.getExpressSeq().equals("")) {
				i = Integer.valueOf(w.getExpressSeq().substring(4));
			}
		}
		i += 1;
		if (i == 999999)
			i = 1;
		return i;
	}

}
